/**
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.api.adwords.starterkit.utils;

import com.mindprod.entities.StripEntities;

import java.util.HashMap;

/**
 * The Class RowUtils is used together with AdWordsReportHandles. It is used 
 * to convert the text fields from AdWords report into others formats
 * 
 * @author rodrigo.f.vale@gmail.com (Rodrigo Vale)
 */
public class RowUtils {
  
  /**
   * Return the value assigned to key as an integer
   *
   * @param row the AdWords report row 
   * @param key the field on the row
   * @param defaultValue the default value in case of error
   * @return the value assigned to key as an integer
   */
  public static int getInt(HashMap<String, String> row, String key, 
      int defaultValue) {
    try {
      return Integer.parseInt(row.get(key));
    } catch (Exception e) {
      return defaultValue;
    }
  }

  /**
   * Return the value assigned to key as boolean
   *
   * @param row the AdWords report row 
   * @param key the field on the row
   * @param defaultValue the default value in case of error
   * @return the value assigned to key as boolean
   */
  public static boolean getBoolean(HashMap<String, String> row, String key, 
      boolean defaultValue) {
    try {
      return Boolean.parseBoolean(row.get(key));
    } catch (Exception e) {
      return defaultValue;
    }
  }

  /**
   * Return the value assigned to key as an long
   *
   * @param row the AdWords report row 
   * @param key the field on the row
   * @param defaultValue the default value in case of error
   * @return the value assigned to key as an long
   */
  public static long getLong(HashMap<String, String> row, String key, 
      long defaultValue) {
    try {
      return Long.parseLong(row.get(key));
    } catch (Exception e) {
      return defaultValue;
    }
  }

  /**
   * Return the value assigned to key as an double
   *
   * @param row the AdWords report row 
   * @param key the field on the row
   * @param defaultValue the default value in case of error
   * @return the value assigned to key as an double
   */
  public static double getDouble(HashMap<String, String> row, String key, 
      double defaultValue) {
    try {
      return Double.parseDouble(row.get(key));
    } catch (Exception e) {
      return defaultValue;
    }
  }

  /**
   * Return the value assigned to key as an string
   *
   * @param row the AdWords report row 
   * @param key the field on the row
   * @param defaultValue the default value in case of error
   * @return the value assigned to key as an string
   */
  public static String getString(HashMap<String, String> row, String key, 
      String defaultValue) {
    try {
      String value = row.get(key);
      if (value == null) {
        value = defaultValue;
      }
      return StripEntities.stripHTMLEntities(value, ' ');
    } catch (Exception e) {
      return defaultValue;
    }
  }
}